package com.world.business.area.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.world.business.area.domain.Area;
import com.world.business.area.domain.query.AreaAddRequest;
import com.world.business.area.domain.query.AreaPageRequest;
import com.world.business.area.domain.query.AreaUpdateRequest;
import com.world.business.area.domain.vo.AreaTreeVO;
import com.world.business.area.domain.vo.AreaVO;
import com.world.business.area.service.IAreaService;
import com.world.common.controller.BaseController;
import com.world.common.domain.ResponseData;
import com.world.common.domain.PageResult;
import com.world.common.domain.SuccessResponseData;
import com.world.common.enumeration.DelFlagEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 地区 前端控制器
 *
 * @author 邓驴
 * @date 2023/05/13 15:46
 */
@Api(tags = "地区")
@RestController
@RequestMapping("/area")
public class AreaController extends BaseController<Area> {

    @Resource
    private IAreaService areaService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public ResponseData<PageResult<AreaVO>> list(AreaPageRequest request) {
        //1.分页条件
        Page<Area> page = startPage(request);

        //2.查询条件
        LambdaQueryWrapper<Area> queryWrapper = Wrappers.lambdaQuery(Area.class);
        queryWrapper.eq(Area::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.eq(StrUtil.isNotBlank(request.getAreaType()), Area::getAreaType, request.getAreaType());
        queryWrapper.like(StrUtil.isNotBlank(request.getAreaName()), Area::getAreaName, request.getAreaName());
        Page<Area> result = areaService.page(page, queryWrapper);

        //3.结果转化
        PageResult<AreaVO> pageResult = getPageResult(result, AreaVO.class);
        return new SuccessResponseData<>(pageResult);
    }

    @ApiOperation("地区树")
    @GetMapping("/tree")
    public ResponseData<List<AreaTreeVO>> tree(AreaPageRequest request) {
        List<AreaTreeVO> tree = areaService.tree(request);
        return new SuccessResponseData<>(tree);
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public ResponseData<Long> add(@RequestBody AreaAddRequest request) {
        Area area = BeanUtil.copyProperties(request, Area.class);
        area.setCreateTime(LocalDateTime.now());
        area.setDelFlag(DelFlagEnum.NORMAL.getCode());
        areaService.save(area);
        return new SuccessResponseData<>(area.getId());
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public ResponseData<Void> update(@RequestBody AreaUpdateRequest request) {
        Area area = BeanUtil.copyProperties(request, Area.class);
        area.setUpdateTime(LocalDateTime.now());
        areaService.updateById(area);
        return new SuccessResponseData<>();
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public ResponseData<Void> delete(@PathVariable("id") Long id) {
        LambdaUpdateWrapper<Area> updateWrapper = Wrappers.lambdaUpdate(Area.class);
        updateWrapper.eq(Area::getId, id);
        updateWrapper.set(Area::getDelFlag, DelFlagEnum.DELETE.getCode());
        areaService.update(updateWrapper);
        return new SuccessResponseData<>();
    }

    @ApiOperation("详情")
    @GetMapping("/get/{id}")
    public ResponseData<AreaVO> get(@PathVariable("id") Long id) {
        Area area = areaService.getById(id);
        if (BeanUtil.isEmpty(area)) {
            return new SuccessResponseData<>();
        }
        AreaVO areaVO = BeanUtil.copyProperties(area, AreaVO.class);
        return new SuccessResponseData<>(areaVO);
    }


}
